<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test importing a JWK key for AES-CBC.");

jsTestIsAsync = true;

var extractable = true;

var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
var plaintext = hexStringToUint8Array("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be6");
var cipherText = ""

var jwkKey = {
    "kty": "oct",
    "alg": "A256CBC",
    "use": "enc",
    "ext": true,
    "k": "YD3rEBXKcb4rc67whX13gR81LAc7YQjXLZgQowkU3_Q"
};

debug("Importing a key...\n");
crypto.subtle.importKey("jwk", jwkKey, {name: 'AES-CBC'}, false, ["encrypt"]).then(function(result) {
    key = result;

    shouldBe("key.type", "'secret'");
    shouldBe("key.extractable", "false");
    shouldBe("key.algorithm.name", "'AES-CBC'");
    shouldBe("key.algorithm.length", "256");
    shouldBe("key.usages", '["encrypt"]');

    debug("\nUsing the key to encrypt plaintext...");
    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, plaintext);
}).then(function(result) {
    encryptionResult = result;
    shouldBe("bytesToHexString(new Uint8Array(encryptionResult))", "'f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d39f23369a9d9bacfa530e26304231461c9aaf02a6a54e9e242ccbf48c59daca6'");
    finishJSTest();
}).then(finishJSTest, failAndFinishJSTest);

</script>

</body>
</html>
